package cn.itcalvin.sncoal.cth.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.itcalvin.sncoal.cth.domain.InfoPfSystemZbDetails;
import cn.itcalvin.sncoal.cth.domain.InfoPfSystemZbDetailsResource;
import cn.itcalvin.sncoal.cth.mapper.main.InfoPfSystemZbDetailsResourceMapper;
import cn.itcalvin.sncoal.cth.pojo.common.ReqLong;
import cn.itcalvin.sncoal.cth.pojo.req.pc.ReqPfZbModify;
import cn.itcalvin.sncoal.cth.pojo.req.pc.ReqPfZbSave;
import cn.itcalvin.sncoal.cth.pojo.req.pf.ReqZbDetailsCl;
import cn.itcalvin.sncoal.cth.pojo.response.pc.RespPfZbDetails;
import cn.itcalvin.sncoal.cth.pojo.response.pf.RespZbDetailsCl;
import cn.itcalvin.sncoal.cth.utils.JwtUtil;
import cn.itcalvin.sncoal.cth.utils.SnowFlakeUtils;
import cn.itcalvin.vo.Message;
import cn.itcalvin.vo.MessageUtil;
import lombok.val;
import org.springframework.stereotype.Service;

import org.springframework.beans.factory.annotation.Autowired;

import cn.itcalvin.sncoal.cth.mapper.main.InfoPfSystemZbDetailsMapper;
import cn.itcalvin.sncoal.cth.service.InfoPfSystemZbDetailsService;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

@Service
public class InfoPfSystemZbDetailsServiceImpl implements InfoPfSystemZbDetailsService{

    @Resource
    private InfoPfSystemZbDetailsMapper infoPfSystemZbDetailsMapper;

    @Resource
    private InfoPfSystemZbDetailsResourceMapper infoPfSystemZbDetailsResourceMapper;

    @Override
    public Message saveMinePfZb(ReqPfZbSave reqPfZbSave) {
        final val userInfo = JwtUtil.getUserInfo();
        infoPfSystemZbDetailsResourceMapper.insert(new InfoPfSystemZbDetailsResource(){{
            setAudioUrl(reqPfZbSave.getAudioUrl());
            setFileUrl(reqPfZbSave.getFileUrl());
            setVideoUrl(reqPfZbSave.getVideoUrl());
            setPictureUrl(reqPfZbSave.getPictureUrl());
            setMineId(userInfo.getBelongId());
            setId(SnowFlakeUtils.getId());
            setCreateId(userInfo.getId());
            setCreateTime(DateUtil.date());
            setIsDelete(false);
            setPfId(reqPfZbSave.getPfId());
            setZbDetailsId(reqPfZbSave.getZbDetailsId());
        }});
        return MessageUtil.success();
    }

    @Override
    public Message modifyMinePfZb(ReqPfZbModify reqPfZbModify) {
        final val userInfo = JwtUtil.getUserInfo();
        InfoPfSystemZbDetailsResource infoPfSystemZbDetailsResource =  new InfoPfSystemZbDetailsResource(){{
            setAudioUrl(reqPfZbModify.getAudioUrl());
            setFileUrl(reqPfZbModify.getFileUrl());
            setVideoUrl(reqPfZbModify.getVideoUrl());
            setPictureUrl(reqPfZbModify.getPictureUrl());
            setId(reqPfZbModify.getId());
            setModifyId(userInfo.getId());
            setModifyTime(DateUtil.date());
        }};
        infoPfSystemZbDetailsResourceMapper.updateByPrimaryKeySelective(infoPfSystemZbDetailsResource);
        return MessageUtil.success();
    }

    @Override
    public Message deleteMinePfZb(ReqLong reqLong) {
        final val userInfo = JwtUtil.getUserInfo();
        final val infoPfSystemZbDetailsResource = infoPfSystemZbDetailsResourceMapper.selectByPrimaryKey(reqLong.getId());
        if(infoPfSystemZbDetailsResource==null){
            return MessageUtil.error("找不到该资料");
        }
        if(!Objects.equals(infoPfSystemZbDetailsResource.getCreateId(), userInfo.getId())){
            return MessageUtil.error("只能删除自己上传的资料");
        }
        infoPfSystemZbDetailsResource.setIsDelete(true);
        infoPfSystemZbDetailsResource.setDeleteId(userInfo.getId());
        infoPfSystemZbDetailsResource.setDeleteTime(DateUtil.date());
        infoPfSystemZbDetailsResourceMapper.updateByPrimaryKeySelective(infoPfSystemZbDetailsResource);
        return MessageUtil.success();
    }

    /**
     * 获取PC端上传佐证材料
     *
     * @param reqLong
     * @return
     */
    @Override
    public Message<RespPfZbDetails> getMinePfZbById(ReqLong reqLong) {
        final val infoPfSystemZbDetailsResource = infoPfSystemZbDetailsResourceMapper.selectByPrimaryKey(reqLong.getId());
        if(infoPfSystemZbDetailsResource!=null){
            return MessageUtil.success(new RespPfZbDetails(){{
                setAudioUrl(infoPfSystemZbDetailsResource.getAudioUrl());
                setFileUrl(infoPfSystemZbDetailsResource.getFileUrl());
                setVideoUrl(infoPfSystemZbDetailsResource.getVideoUrl());
                setPictureUrl(infoPfSystemZbDetailsResource.getPictureUrl());
                setId(infoPfSystemZbDetailsResource.getId());
                setPfId(infoPfSystemZbDetailsResource.getPfId());
                setZbDetailsId(infoPfSystemZbDetailsResource.getZbDetailsId());
                setMineId(infoPfSystemZbDetailsResource.getMineId());
            }});
        }
        return MessageUtil.error("找不到该资料");
    }

    /**
     * 获取PC端上传佐证材料列表
     *
     * @param reqZbDetailsCl
     * @return
     */
    @Override
    public List<RespZbDetailsCl> getZbDetailsResourcePcList(ReqZbDetailsCl reqZbDetailsCl) {
         return infoPfSystemZbDetailsMapper.getZbDetailsResourcePcList(reqZbDetailsCl.getZbDetailsId(), reqZbDetailsCl.getMineId());
    }
}
